#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "naivebinarytree.h"




int mybinarytredicate(void* xx, void* yy){
	int* x = (int*)xx;
	int* y = (int*)yy;	
	if(*x >*y)
		return 1;
	if(*x <*y)
		return -1;
	else
		return 0;
}
void mydealloc(void* xx, void* yy){

};

void show( bnode* node, int l){
	int x;
	if(node){
		l++;
		show(node->left,l );
		if(node->parent)
			x=*((int*)node->parent->value);
		else
			x=0;
		printf("%d %d\n", *((int*)node->value),x);
		show(node->right, l);
	}
}

void shownode(void* k, void* v){
	printf("key:%d value:%d\n",*((int*)k), *((int*)v));
}


int main (int argv, char** argc){
	bnode* rootnode=0;
	bnode* reply=0;


	int x2 =6;
	tree_insert(&rootnode,&x2, &x2, mybinarytredicate);

	int x3 =17;
	tree_insert(&rootnode,&x3, &x3, mybinarytredicate);

	int x4 =13;
	tree_insert(&rootnode,&x4, &x4, mybinarytredicate);

	int x5 =12;
	tree_insert(&rootnode,&x5, &x5, mybinarytredicate);

	int x6 =11;
	tree_insert(&rootnode,&x6, &x6, mybinarytredicate);

	tree_inorder_walk(rootnode,  shownode);
	show(rootnode, 0);
	exit(0);

	int x8 =15;
	tree_insert(&rootnode,&x8, &x8, mybinarytredicate);

	int x9 =14;
	tree_insert(&rootnode,&x9, &x9, mybinarytredicate);

	int x10 =16;
	tree_insert(&rootnode,&x10, &x10, mybinarytredicate);

	int x11 =20;
	tree_insert(&rootnode,&x11, &x11, mybinarytredicate);

	int x12 =8;
	tree_insert(&rootnode,&x12, &x12, mybinarytredicate);

	int x13 =3;
	tree_insert(&rootnode,&x13, &x13, mybinarytredicate);
	//int x12 =20; printf("%d\n",tree_insert(&rootnode,&x12, &x12, mybinarytredicate));
	//int x13 =21; printf("%d\n",tree_insert(&rootnode,&x13, &x13, mybinarytredicate)); exit(0);


	show(rootnode, 0);
	

	int d;
	
	int qq =8;
	if(reply=tree_search(rootnode,(void*)&qq, mybinarytredicate)){
		printf("found\n");
	}
	else{
		printf("NOT found\n");

	}

	d =6;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	printf("xx\n");show(rootnode, 0);

	d =11;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	printf("xx\n");show(rootnode, 0);






	d =12;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	show(rootnode, 0);

	d =13;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	show(rootnode, 0);

	d =14;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	show(rootnode, 0);
	
	d =15;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);


	show(rootnode, 0);

	d =16;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	d =16;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);
	show(rootnode, 0);

	d =17;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	show(rootnode, 0);

	d =20;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	show(rootnode, 0);

	d =6;
	tree_delete(&rootnode, (void*)&d, mybinarytredicate, mydealloc);

	printf("yy\n");show(rootnode, 0);


	sleep(10);



	int q =16;
	if(reply=tree_search(rootnode,(void*)&q, mybinarytredicate)){
		printf("found\n");
	}
	else{
		printf("NOT found\n");

	}

	tree_destroy(&rootnode,  mybinarytredicate, mydealloc);
	show(rootnode, 0);

}
